<html>
<head>js替换URL参数</head>
<body>


<button onclick="changeURLArgReload()">点击我</button>

<script>
    function changeURLArgReload() {
        // 获取当前页面的URL地址
        var oUrl = this.location.href.toString();
        // 获取参数的值
        var pam = getURLString("aaa");
        console.log("pam===>" + pam)
        // 将参数替换成新参数返回URL
        var nUrl = changeURLArg(oUrl, "aaa", pam + "asdfw5456")
        // 将页面的URL跳转到新的URL
        this.location = nUrl;
        window.location.href = nUrl
    }

    /**
     * 获取URL中的参数值
     * @param arg
     * @returns {string|null}
     */
    function getURLString(arg) {
        var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }

    /**
     * 替换URL参数值为新值
     * @param paramName
     * @param replaceWith
     */
    function replaceParamVal(paramName, replaceWith) {
        var oUrl = this.location.href.toString();
        var re = eval('/(' + paramName + '=)([^&]*)/gi');
        console.log("re===>" + re)
        var nUrl = oUrl.replace(re, paramName + '=' + replaceWith);
        this.location = nUrl;
        window.location.href = nUrl
    }

    /**
     * 替换URL中的值返回新的URL
     * @param url
     * @param arg
     * @param arg_val
     * @returns {string|*}
     */
    function changeURLArg(url, arg, arg_val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + arg_val;
        if (url.match(pattern)) {
            var tmp = '/(' + arg + '=)([^&]*)/gi';
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match('[\?]')) {
                return url + '&' + replaceText;
            } else {
                return url + '?' + replaceText;
            }
        }
    }
</script>
</body>
</html>